<?php include('php/authenticatephastregex.php'); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en-US">
  <head>
    <title>Phastregex. in-silico chip-seq</title>
    <meta http-equiv="Content-Type" content ="text/html; charset=utf-8">
    <link rel="stylesheet" href="phastregexstylejw.css" type="text/css">
    <?php include('php/jasonweirather.php'); ?>
  </head>
  <body>
    <?php include('php/logoline.php'); ?>
    <div id = "hit_results_block">
    <?php
      $regex = $_POST["regex"];
      $species = $_POST["speciesdropdown"];
      $title = $_POST["title"];      
      echo("$title<br>");
      echo("Species genome: $species<br>");
      echo("Regular expression: $regex<br>");
      ### check to see if it is in the database and completed already.
      $database = 'zebraphish';
      include("php/dbconnect.php");
      include("php/filehandles.php");
      $hitfile = "$basename" . "_hits.txt";
    if($_SESSION['hit_file_generated'] == 1) {}
    else {
      $query = "SELECT completed, db_id FROM regex_scan WHERE species = '$species' AND regex = '$regex'";
      $result = mysql_query($query);
      $row = mysql_fetch_object($result);
      $regex_id = NULL;
      if($row == NULL) {
        echo "This regular expression has not been run.<br>\n";
        echo "Please be patient while the search is run and output generated.";
        flush(); #display so far
        sleep(1);

        $regexfilename = "$workingdirectory/$basename.regex";
        $fh = fopen($regexfilename,'w') or die ("can't open file");
        fwrite($fh,$regex);
        fclose($fh);
        #$evalue = whatever
        $titlefilename = "$workingdirectory/$basename.title";
        $fh = fopen($titlefilename,'w') or die ("can't open file");
        fwrite($fh,$title);
        fclose($fh);
        $command = "cat $regexfilename | $phastregex2path/locate_matches.pl $species";
        echo "executing: $command <br>\n";
        $lines = `$command`;

        $query = "SELECT completed, db_id FROM regex_scan WHERE species = '$species' AND regex = '$regex'";
        $result = mysql_query($query);
        $row = mysql_fetch_object($result);
        #echo "Genome-wide search complete, assigned search id $regex_id.<br>";


      }
      $completed = $row->completed;
      if($completed == 0) {
        # the job has not been completed yet.  Is it still running or did it stall or what?
        echo('incomplete search.  Wait more or try to restart job? Work on this....<br>');
        die;
      } else { # it is finished.
        $regex_id = $row->db_id;
        echo "Genome-wide search complete, assigned search id $regex_id.<br>";
        flush(); #display so far
        sleep(1);
        #$command = "$phastregex2path/print_hits.pl $regex_id > $workingdirectory/$hitfile";
        ############# to get that to work you have to set permission to allow select from www-data on zebraphish
        $command = "$phastregex2path/print_hits.pl $regex_id > $workingdirectory/$hitfile";
        echo "executing: $command <br>\n";
        #$lines = `$command`;
        $lines = `$command`;
      }
      $_SESSION['hit_file_generated'] = 1;
    }
      echo("Hits are availible for download:<br>");
      echo("<a href = \"$working_address/$hitfile\">$working_address/$hitfile</a><br>");


        $query = "SELECT completed, db_id FROM regex_scan WHERE species = '$species' AND regex = '$regex'";
        $result = mysql_query($query);
        $row = mysql_fetch_object($result);
        $regex_id = $row->db_id;

      $query = "SELECT completed FROM conservation_scan WHERE regex_id = $regex_id";
      $result = mysql_query($query);
      $row = mysql_fetch_object($result);
      $do_conseravtion = 0;
      if($row == NULL) { # need to compute conservation.
        $do_conservation = 1;
      } else {
        if($row->completed == 0) {
          echo("problem didnt finish a conservation scan for regex_id $regex_id !<br>\n");
          die;
        }
      }
      if($do_conservation == 1) { # scan for conservation for this regular expression.
        echo("please be patient while we calculate conservation for all hits.<br>\n");
        flush();
        sleep(1);
        $command = "$phastregex2path/calculate_all_conservation.pl $regex_id";
        echo "executing: $command <br>\n";
        $lines = `$command`;
      }
      $query = "SELECT completed FROM conservation_scan WHERE regex_id = $regex_id";
      $result = mysql_query($query);
      $row = mysql_fetch_object($result);
      if($row == NULL) { 
        echo "problem calculating conservation, even though it seemed we had for regex_id $regex_id<br>\n";
        die;
      } else {
        if($row->completed == 0) {
          echo "problem calculating conservation, even though it seemed we had for regex_id $regex_id<br>\n";
          die;
        }
      }
      echo("conservation has been calculated for all hits.<br>\n");
      echo("Filter output based on conservation or just submit for all hits with any conseravtion.  Set both if you want to filter..<br>\n");
      # If we get to here, we have the hit file, and now we have got the conservation data.
      ### this is the fun part, options for output.        
      ### generate a file of all conserved hits and permit filtering based on the database.
      echo('
        <form enctype="multipart/form-data" action="conservedresults.php" method="post">
            <span class="form_label">Select conservation database:<br></span><select id="conservation_db_menu" name="conservation_db">
                 <option value="any" selected="yes">no requirement</option>
                 <option value="placental">placental</option>
                 <option value="vertebrate">vertebrate</option>
               </select>
            <span class="form_label">Select conservation level:<br></span><select id="conservation_level_menu" name="conservation_level">
                 <option value="any" selected="yes">no requirement</option>
                 <option value="05_1">low (at least 0.05-0.1)</option>
                 <option value="4_5">medium (at least 0.4-0.5)</option>
                 <option value="8_9">high (at least 0.8-0.9)</option>
               </select>
            <input type="hidden" name="regex" value="'.$regex.'">
            <input type="hidden" name="regex_id" value="'.$regex_id.'">
            <input type="hidden" name="species" value="'.$species.'">
            <input type="hidden" name="title" value="'.$title.'">
            <input type="submit" value="Submit"/>
        </form>
       ');

    ?>
    </div>
    <?php include('php/lastline.php'); ?>
  </body>
</html> 
